<?php
/**
 * Created by PhpStorm.
 * User: chenxiongde
 * Date: 17/4/30
 * Time: 下午3:04
 */


final class MANGOPHP{
    public  static function  run(){
        self::_set_const();
        defined('DEBUG') || debug('DEBUG',false);
        if(DEBUG){
            self::_create_dir();
            self::_import_files();
        }else{
            error_reporting(0);
            require_once TEMP_PATH.'/_boot.php';
        }

        Application::run();
    }

    /**
     * 设置常量
     */
    private static function  _set_const(){
        $path =str_replace('\\','/',__FILE__);
        define('MANGO_ROOT_PATH',dirname($path));
        define('MANGO_CONF_PATH',MANGO_ROOT_PATH.'/Conf');
        define('MANGO_DATA_PATH',MANGO_ROOT_PATH.'/Data');
        define('MANGO_TPL_PATH',MANGO_DATA_PATH.'/Tpl');
        define('MANGO_LIB_PATH',MANGO_ROOT_PATH.'/Lib');
        define('MANGO_CORE_PATH',MANGO_LIB_PATH.'/core');
        define('MANGO_FUNCTION_PATH',MANGO_LIB_PATH.'/function');
        define('TEMP_PATH',dirname(MANGO_ROOT_PATH).'/Temp');
        define('LOG_PATH',TEMP_PATH.'/Log');
        define('ROOT_PATH',dirname(MANGO_ROOT_PATH));
        define('APP_PATH',ROOT_PATH.'/'.APP_NAME);
        define('APP_CONF_PATH',APP_PATH.'/Conf');
        define('APP_CONTROLLER_PATH',APP_PATH.'/Controller');
        define('APP_MODEL_PATH',APP_PATH.'/Model');
        define('APP_PUBLIC_PATH',APP_PATH.'/Public');
    }

    /**
     * 创建应用文件夹
     */
    private static   function   _create_dir(){
        $app_dirs = array(
            TEMP_PATH,
            LOG_PATH,
            APP_PATH,
            APP_CONF_PATH,
            APP_CONTROLLER_PATH,
            APP_MODEL_PATH,
            APP_PUBLIC_PATH
        );
        foreach($app_dirs as $v){
            is_dir($v)||mkdir($v,0777,true);
        }
    }

    private static  function   _import_files(){
        $files = array(
            MANGO_FUNCTION_PATH.'/function.php',
            MANGO_CORE_PATH.'/Controller.class.php',
            MANGO_CORE_PATH.'/Log.class.php',
            MANGO_CORE_PATH.'/Application.class.php',
        );
        $str = '';
        foreach($files as $v){
            $str.=trim(substr(file_get_contents($v),5,-2))."".PHP_EOL;
            require_once $v;
        }
        $str = "<?php\r\n" .$str;
        file_put_contents(TEMP_PATH.'/_boot.php',$str) || die('access not allow!');
    }
}

MANGOPHP::run();